package net.esj.basic.core.engine.support.validator.constraints.validator;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import net.esj.basic.core.engine.support.validator.constraints.CompareNumber;


public class CompareNumberValidator implements ConstraintValidator<CompareNumber,Number[]> {
	
	private CompareMode mode;
	
	@Override
	public void initialize(CompareNumber arg0) {
		mode = arg0.value();
	}
	
	@Override
	public boolean isValid(Number[] object, ConstraintValidatorContext context) {
		if(object==null){
			return true;
		}
		if(object[0]==null || object[1]==null){
			return true;
		}
		Number first = object[0];
		Number second = object[1];
		switch(mode){
		case EQUAL:
			if(first.doubleValue()==second.doubleValue()){
				return true;
			}else{
				return false;
			}
		case GREATER:
			if(first.doubleValue()> second.doubleValue()){
				return true;
			}else{
				return false;
			}
		case GREATER_EQUAL:
			if(first.doubleValue()>= second.doubleValue()){
				return true;
			}else{
				return false;
			}
		case LOWER:
			if(first.doubleValue()< second.doubleValue()){
				return true;
			}else{
				return false;
			}
		case LOWER_EQUAL:
			if(first.doubleValue()<= second.doubleValue()){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

	
	
	public static enum CompareMode{
		GREATER,
		GREATER_EQUAL,
		LOWER,
		LOWER_EQUAL,
		EQUAL;
	}

}
